package com.ruoyi.law.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageCondition;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.law.annotation.Statistics;
import com.ruoyi.law.domain.dto.LawNewsDTO;
import com.ruoyi.law.domain.entity.LawNews;
import com.ruoyi.law.service.ILawNewsService;
import com.ruoyi.law.service.ILawNewsTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 新闻 前端控制器
 * </p>
 *
 * @author code
 * @since 2022-12-11
 */
@Api(tags = "新闻")
@RestController
@RequestMapping("/news")
public class LawNewsController extends BaseController {

    @Autowired
    private ILawNewsService newsService;
    @Autowired
    private ILawNewsTypeService newsTypeService;

    @Anonymous
    @ApiOperation("列表")
    @GetMapping("/list")
    public R<Page<LawNewsDTO>> list(PageCondition<LawNews> pageCondition) {
        Page<LawNewsDTO> page = newsService.myPage(pageCondition.toPage());
        return R.ok(page);
    }

    @Statistics
    @Anonymous
    @ApiOperation("详情")
    @GetMapping("/info")
    public R<LawNewsDTO> info(Long id) {
        LawNews lawNews = newsService.getById(id);
        LawNewsDTO lawNewsDTO = BeanUtil.toBean(lawNews, LawNewsDTO.class);
        lawNewsDTO.setTypeName(newsTypeService.getNameById(lawNewsDTO.getType()));
        return R.ok(lawNewsDTO);
    }

    @ApiOperation("新增")
    @PostMapping("/save")
    public R<Object> save(@RequestBody LawNews lawNews) {
        newsService.save(lawNews);
        return R.ok();
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    public R<Object> update(@RequestBody LawNews lawNews) {
        newsService.updateById(lawNews);
        return R.ok();
    }

    @ApiOperation("删除")
    @PostMapping("/delete")
    public R<Object> delete(@RequestBody Long id) {
        newsService.removeById(id);
        return R.ok();
    }

    @Anonymous
    @ApiOperation("根据新闻类型id查询新闻列表")
    @ApiImplicitParam(name = "typeId", value = "新闻类型id", required = true, dataType = "Long")
    @GetMapping("/getBytypeId")
    public R<List<LawNewsDTO>> getBytypeId(Long typeId) {
        List<LawNewsDTO> list = newsService.getBytypeId(typeId);
        return R.ok(list);
    }

}
